package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author：徐志荣
 * @Date：2023/7/9 上午 11:16
 */

@RestController
public class UserController {
    private final static String SESSION_KEY = "USER_SESSION_KEY";
    @RequestMapping("/login")
    public String login(HttpSession session){
        //存储session 到redis
        session.setAttribute(SESSION_KEY,"fff");
        return "login success";
    }

    @RequestMapping("/get")
    public String get(HttpServletRequest request){
        String username = "";
        HttpSession session = request.getSession(false);
        if(session!=null){
            //从 redis 中获取 session用户
           Object s =  session.getAttribute(SESSION_KEY);
           if(s != null){
               return s.toString();
           }
        }
        return username;
    }
}
